﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
    public class ParameterDAO
    {
        private ParameterDTO CreateParameterDTOFromEntity(ThamSo ts)
        {
            ParameterDTO paraDto = new ParameterDTO
            {
                ParameterID = ts.ThamSoID,
                ParameterCode = ts.MaThamSo,
                ParameterName = ts.TenThamSo,
                Value = int.Parse(ts.GiaTri.ToString()),
                Status = bool.Parse( ts.SuDung.ToString())
            };

            return paraDto;
        }

        public int GetParameterValueByCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ThamSo para = ent.ThamSoes.FirstOrDefault(p => p.MaThamSo == code);

                    return para != null ? int.Parse(para.GiaTri.ToString()) : -1;
                }
            }
            catch
            {
                return -1;
            }
        }

        public ParameterDTO GetParameterByID(int id)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ThamSo ts = ent.ThamSoes.FirstOrDefault(p => p.ThamSoID == id);

                    return CreateParameterDTOFromEntity(ts);
                }
            }
            catch
            {
                return null;
            }
        }

        public int InsertParameter(ParameterDTO paramDto)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                ThamSo paramInt = new ThamSo
                {
                    MaThamSo = paramDto.ParameterCode,
                    TenThamSo = paramDto.ParameterName,
                    GiaTri = paramDto.Value,
                    SuDung = true
                };

                if (ent.ThamSoes.Count(t => t.ThamSoID != null) > 0)
                {
                    paramInt.ThamSoID = ent.ThamSoes.Max(t => t.ThamSoID) + 1;
                }
                else
                {
                    paramInt.ThamSoID = 1;
                }

                ent.AddToThamSoes(paramInt);

                DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                docSeqDAO.UpdateNextDocSequence(DocSequence.Parameter.ToString());

                ent.SaveChanges();

                return paramInt.ThamSoID;
            }
        }

        public List<ParameterDTO> GetParameterList()
        {
            List<ParameterDTO> listParameterDto = new List<ParameterDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<ThamSo> listThamSo = ent.ThamSoes.ToList();
                foreach (ThamSo item in listThamSo)
                {
                    ParameterDTO paramDto = CreateParameterDTOFromEntity(item);
                    listParameterDto.Add(paramDto);
                }
            }
            return listParameterDto;
        }

        public BindingList<ParameterDTO> GetNewBindingListParameter()
        {
            BindingList<ParameterDTO> listParameterDto = new BindingList<ParameterDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<ThamSo> listThamSo = ent.ThamSoes.ToList();
                foreach (ThamSo item in listThamSo)
                {
                    ParameterDTO paramDto = CreateParameterDTOFromEntity(item);
                    listParameterDto.Add(paramDto);
                }
            }
            return listParameterDto;
        }

        public bool UpdateParameter(ParameterDTO paramDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ThamSo paramEnt = ent.ThamSoes.FirstOrDefault(c => c.ThamSoID == paramDTO.ParameterID);

                    paramEnt.TenThamSo = paramDTO.ParameterName;
                    paramEnt.GiaTri = paramDTO.Value;
                    paramEnt.SuDung = paramDTO.Status;
                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteParameter(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ThamSo paramEnt = ent.ThamSoes.FirstOrDefault(c => c.ThamSoID == ID);
                    paramEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.ThamSoes.Where(c => c.MaThamSo == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }
    }
}
